var count = 0;
// 指定图表的配置项和数据
var option = {
    title: {
        text: '数据统计'
    },
    tooltip: {
        trigger: 'axis'
    },
    toolbox: {
        feature: {
            saveAsImage: {
                title:'保存'
            }
        }
    },
    legend: {
        data:[],
        y:'bottom'
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: []
    },
    yAxis: {
        type: 'value',
    },
    series: []
};
var option1 = clone(option);
var option2 = clone(option);
var option3 = clone(option);
var option4 = clone(option);
var option5 = clone(option);
var status = null;

function clone(obj){
    return JSON.parse(JSON.stringify(obj))
}
function draw(){
    chartData = sourceData.map(function(a){
        return a.slice(count,parseInt(globalOptions.dataLength)+count)
    })
    if(chartMenu === 'vibration'){
        option.title.text = "振动数据统计"
        option.legend.data = ["振动X","振动Y","振动Z"]
        option.xAxis.data = chartData[mirrorMenu['时间']];
        option.series = null;
        option.series = [];
        option.series.push(
            {name: '振动X',type: 'line',data: chartData[mirrorMenu['振动X']]},
            {name: '振动Y',type: 'line',data: chartData[mirrorMenu['振动Y']]},
            {name: '振动Z',type: 'line',data: chartData[mirrorMenu['振动Z']]}
        )
        $("#chart_info").html("振动X: <span class='info'>"+option.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 振动Y: <span class='info'>"+option.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span> 振动Z: <span class='info'>"+option.series[2].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    }else if(chartMenu === 'pressure'){
        option.title.text = "压力数据统计"
        option.legend.data = ["管内压力","环空压力"]
        option.xAxis.data = chartData[mirrorMenu['时间']];
        option.series = [];
        option.series.push(
            {name: '管内压力',type: 'line',data: chartData[mirrorMenu['管内压力']]},
            {name: '环空压力',type: 'line',data: chartData[mirrorMenu['环空压力']]},{name:1,type:'line',data:[]}
        )
        $("#chart_info").html("管内压力: <span class='info'>"+option.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 环空压力: <span class='info'>"+option.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    }else if(chartMenu === 'temperature'){
        option.title.text = "温度数据统计"
        option.legend.data = ["管内温度","环空温度"]
        option.xAxis.data = chartData[mirrorMenu['时间']];
        option.series = [];
        option.series.push(
            {name: '管内温度',type: 'line',data: chartData[mirrorMenu['管内温度']]},
            {name: '环空温度',type: 'line',data: chartData[mirrorMenu['环空温度']]},{name:1,type:'line',data:[]}
        )
        $("#chart_info").html("管内温度: <span class='info'>"+option.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 环空温度: <span class='info'>"+option.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    }else if(chartMenu === 'drilling'){
        option.title.text = "钻压数据统计"
        option.legend.data = ["钻压"]
        option.xAxis.data = chartData[mirrorMenu['时间']];
        option.series = [];
        option.series= [{name: '钻压',type: 'line',data: chartData[mirrorMenu['钻压']]},
            {name:1,type:'line',data:[]},
            {name:1,type:'line',data:[]}]
        $("#chart_info").html("钻压: <span class='info'>"+option.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    }else if(chartMenu === 'torque'){
        option.title.text = "扭矩数据统计"
        option.legend.data = ["扭矩"]
        option.xAxis.data = chartData[mirrorMenu['时间']];
        option.series = [];
        option.series= [{name: '扭矩',type: 'line',data: chartData[mirrorMenu['扭矩']]},
            {name:1,type:'line',data:[]},
            {name:1,type:'line',data:[]}]
        $("#chart_info").html("扭矩: <span class='info'>"+option.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    }
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
    count += 10
}
function start() {
    $("#startButton").attr("disabled","disabled");
    status = setInterval(function(){
        draw()
    },parseInt(globalOptions.speed))
}
function pause() {
    $("#startButton").removeAttr("disabled");
    clearInterval(status);
}
function stop() {
    $("#startButton").removeAttr("disabled");
    clearInterval(status);
    count = 0;
    draw();
}
function clear1() {
    $("#startButton").removeAttr("disabled");
    var noChart = ["home","more","setting"]
    clearInterval(status);
    count = 0;
    if(noChart.indexOf(chartMenu)<0){
        draw()
    }
}
function moreClear() {
    $("#startMoreButton").removeAttr("disabled");
    clearInterval(status);
    count = 0;
    if(chartMenu === 'more'){
        moreDraw()
    }
}
//多视图模式
function startMore() {
    $("#startMoreButton").attr("disabled","disabled");
    status = setInterval(function(){
        moreDraw()
    },parseInt(globalOptions.speed))
}
function pauseMore() {
    $("#startMoreButton").removeAttr("disabled");
    clearInterval(status);
}
function stopMore() {
    $("#startMoreButton").removeAttr("disabled");
    clearInterval(status);
    count = 0;
    moreDraw();
}
function moreDraw() {

    chartData = sourceData.map(function(a){
        return a.slice(count,parseInt(globalOptions.dataLength)+count)
    })

    option1.title.text = "压力数据统计"
    option1.legend.data = ["管内压力","环空压力"]
    option1.xAxis.data = chartData[mirrorMenu['时间']];
    option1.series = [];
    option1.series.push(
        {name: '管内压力',type: 'line',data: chartData[mirrorMenu['管内压力']]},
        {name: '环空压力',type: 'line',data: chartData[mirrorMenu['环空压力']]}
    )

    option2.title.text = "温度数据统计"
    option2.legend.data = ["管内温度","环空温度"]
    option2.xAxis.data = chartData[mirrorMenu['时间']];
    option2.series = [];
    option2.series.push(
        {name: '管内温度',type: 'line',data: chartData[mirrorMenu['管内温度']]},
        {name: '环空温度',type: 'line',data: chartData[mirrorMenu['环空温度']]}
    )

    option3.title.text = "钻压数据统计"
    option3.legend.data = ["钻压"]
    option3.xAxis.data = chartData[mirrorMenu['时间']];
    option3.series = [];
    option3.series= [{name: '钻压',type: 'line',data: chartData[mirrorMenu['钻压']]}]

    option4.title.text = "扭矩数据统计"
    option4.legend.data = ["扭矩"]
    option4.xAxis.data = chartData[mirrorMenu['时间']];
    option4.series = [];
    option4.series= [{name: '扭矩',type: 'line',data: chartData[mirrorMenu['扭矩']]}]

    option5.title.text = "振动数据统计"
    option5.legend.data = ["振动X","振动Y","振动Z"]
    option5.xAxis.data = chartData[mirrorMenu['时间']];
    option5.series = null;
    option5.series = [];
    option5.series.push(
        {name: '振动X',type: 'line',data: chartData[mirrorMenu['振动X']]},
        {name: '振动Y',type: 'line',data: chartData[mirrorMenu['振动Y']]},
        {name: '振动Z',type: 'line',data: chartData[mirrorMenu['振动Z']]}
    )
    // 使用刚指定的配置项和数据显示图表。
    myChart1.setOption(option1);
    myChart2.setOption(option2);
    myChart3.setOption(option3);
    myChart4.setOption(option4);
    myChart5.setOption(option5);
    $("#chart1_info").html("管内压力: <span class='info'>"+option1.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 环空压力: <span class='info'>"+option1.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    $("#chart2_info").html("管内温度: <span class='info'>"+option2.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 环空温度: <span class='info'>"+option2.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    $("#chart3_info").html("钻压: <span class='info'>"+option3.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    $("#chart4_info").html("扭矩: <span class='info'>"+option4.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    $("#chart5_info").html("振动X: <span class='info'>"+option5.series[0].data[parseInt(globalOptions.dataLength) -1]+"</span> 振动Y: <span class='info'>"+option5.series[1].data[parseInt(globalOptions.dataLength) -1]+"</span> 振动Z: <span class='info'>"+option5.series[2].data[parseInt(globalOptions.dataLength) -1]+"</span>")
    count += 10
}